package com.sanweibook.jhook.web.sys;

import com.sanweibook.jhook.common.Constants;
import com.sanweibook.jhook.common.util.JhookObjectUtils;
import com.sanweibook.jhook.redis.core.RedisClientTemplate;
import com.sanweibook.jhook.shiro.realm.AuthorUser;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by twg on 16/11/2.
 */
@Slf4j
@Controller
@RequestMapping("/admin/home")
public class HomeController {
    @Autowired
    private RedisClientTemplate redisClientTemplate;


    @RequestMapping
    @RequiresPermissions("sys:menu:list")
    public String home(Model model,HttpServletRequest request){
        if(log.isDebugEnabled()){
            log.debug(" uri : {}",request.getRequestURI());
        }
        Subject subject = SecurityUtils.getSubject();
        AuthorUser authorUser = JhookObjectUtils.jsonToObject((String) redisClientTemplate.hGet(Constants.JHOOK_AUTHOR_USER,subject.getPrincipal().toString()),AuthorUser.class);
        model.addAttribute("authorUser",authorUser);
        return "home";
    }

    @RequestMapping("index")
    public String index(){
        return "index";
    }
}
